package com.cjh.controller;


import com.cjh.pojo.Menu;
import com.cjh.pojo.Type;
import com.cjh.service.MenuService;
import com.cjh.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cjh
 * @since 2020-12-17
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private TypeService typeService;

    @Value("${server.port}")
    private String port;

    @GetMapping("/index")
    public String index() {
        return "menu-8020的端口号是：" + port;
    }

    @GetMapping("/menuList")
    public PageInfo<Menu> menuList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        PageHelper.startPage(page, limit);
        List<Menu> menus = menuService.selectMenusPaging();
        PageInfo<Menu> pageInfo = new PageInfo<>(menus);

        return pageInfo;
    }

    @PostMapping("/insertMenu")
    @HystrixCommand(fallbackMethod = "hystrixInsertMenu")
    public boolean insertMenu(@RequestBody Menu menu){
        return menuService.save(menu);
    }
    public boolean hystrixInsertMenu(@RequestBody Menu menu){
        return false;
    }

    @DeleteMapping("/deleteMenuById/{id}")
    public boolean deleteMenuById(@PathVariable("id") Integer id){
        return menuService.removeById(id);
    }

    @GetMapping("/selectMenuById/{id}")
    public Menu selectMenuById(@PathVariable("id") Integer id) {
        return menuService.getById(id);
    }

    @PutMapping("/updateMenuById")
    public boolean updateMenuById(@RequestBody Menu menu) {
        return menuService.updateById(menu);
    }

    @GetMapping("/typeList")
    public List<Type> typeList(){
        return typeService.list();
    }
}

